<template>
	<view>
		<baseHead title="基本信息" bg="#ffffff" color="#000000" :right="true" text_right="保存" @clickRight="clickRight">
		</baseHead>

		<view class="commview">
			<u--form labelPosition="left" :model="model" ref="form1" labelWidth="100">

				<u-form-item label="企业全称" required borderBottom ref="item1">
					<u--input v-model="model.FullName" border="none" placeholder="请输入" inputAlign="right"
						:disabled="true" disabledColor="#fff"></u--input>
					<u-icon slot="right" color="#ffffff" name="arrow-right"></u-icon>
				</u-form-item>

				<u-form-item label="企业简称" required borderBottom ref="item1">
					<u--input v-model="model.ShortName" disabledColor="#ffffff" placeholder="请输入" inputAlign="right"
						border="none">
					</u--input>
					<u-icon slot="right" color="#ffffff" name="arrow-right"></u-icon>
				</u-form-item>

				<u-form-item label="所属行业" required borderBottom ref="item1">
					<uni-data-picker :localdata="items5" v-model="model.Industry" :isAdd="true" class="unidatapickert"
						popup-title="所属行业" ref="picker" :clear-icon="false" @change="onchange">
					</uni-data-picker>

					<u-icon name="arrow-right"></u-icon>
				</u-form-item>

				<u-form-item label="企业性质" required borderBottom @click="show1 = true;" ref="item1">
					<u--input :value="columns1value" disabled style="pointer-events:none" disabledColor="#ffffff"
						placeholder="请选择" inputAlign="right" border="none">
					</u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-form-item label="融资阶段" required borderBottom @click="show2 = true;" ref="item1">
					<u--input :value="columns2value" disabled style="pointer-events:none" disabledColor="#ffffff"
						placeholder="请选择" inputAlign="right" border="none">
					</u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-form-item label="人员规模" required borderBottom @click="show3 = true; " ref="item1">
					<u--input :value="columns3value" disabled style="pointer-events:none" disabledColor="#ffffff"
						placeholder="请选择" inputAlign="right" border="none">
					</u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
			</u--form>

			<view class="commtpddsview">
				<view class="">
					<text style="color: red;">*</text> 企业简介
				</view>
				<view style="margin: 20rpx 0rpx;">
					<u--textarea v-model="model.Description" placeholder="请输入企业简介" count maxlength="300"></u--textarea>
				</view>
			</view>

			<view class="commtpddsview">
				<view class="adddz">
					<view>
						<text style="color: red;">*</text>企业地址
					</view>
					<view>
						<text @click="addaddlist">
							<u-icon name="plus"></u-icon>
						</text>
					</view>
				</view>

				<view class="addnrdz" v-for="(intem,index) in model.Address" :key="index">
					<view @click="xxAddress(intem,index)">
						<u--input v-model="intem.Address" disabled style="pointer-events:none" disabledColor="#ffffff"
							placeholder="请选择详细地址">
						</u--input>
					</view>

					<view class="addndax">
						<view>
							<u-checkbox-group placement="column" @change="checkboxChange(intem,index)">
								<u-checkbox :customStyle="{marginBottom: '8px'}" :checked="intem.IsDefault"
									label="设为默认地址">
								</u-checkbox>
							</u-checkbox-group>
						</view>
						<view style="padding-right: 40rpx;">
							<view @click="shanchulist(index)">
								<u-icon name="minus"></u-icon>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<u-picker :show="show1" title="企业性质" :columns="columns1" @close="show1 = false;" @cancel="show1 = false;"
			@confirm="confirm1"></u-picker>
		<u-picker :show="show2" title="融资阶段" :columns="columns2" @close="show2 = false;" @cancel="show2 = false;"
			@confirm="confirm2"></u-picker>
		<u-picker :show="show3" title="人员规模" :columns="columns3" @close="show3 = false;" @cancel="show3= false;"
			@confirm="confirm3"></u-picker>

		<addPopup :show.sync='addShow' v-if="addShow" :addTitle='addTitle' :addxxAddressItem='addxxAddressItem' @editAddress="editAddress"></addPopup>

	</view>
</template>

<script>
	import baseHead from '@/pages/component/baseHead.vue'
	import pickRegions from '@/pages/component/pick-regions/pick-regions.vue'
	import area from '@/pages/component/area.js'
	import addPopup from '@/pages/component/addPopup/index.vue'
	export default {
		data() {
			return {
				URL: this.$config.domain.staticUrl,
				model: {
					FullName: "",
					ShortName: "",
					Industry: "",
					Category: "",
					Stage: "",
					Scale: "",
					qiyigm: "",
					Description: "",
					ProvinceCode: "",
					CityCode: "",
					AreaCode: "",
					PcaAddress: "",
					Address: [{
						Address: "",
						AreaCode: "",
						CityCode: "",
						CorpId: "",
						FullName: "",
						IsDefault: false,
						//经度
						Longitude: 0,
						//纬度
						Latitude: 0,
						PcaAddress: "",
						ProvinceCode: "",
						Source: 0,
					}]
				},

				//经度
				Longitude: 0,
				//纬度
				Latitude: 0,

				items5: [],
				zwVal: "3",
				show1: false,
				columns1: [],
				show2: false,
				columns2: [],
				show3: false,
				columns3: [],

				addShow: false,
				addTitle: '企业地址',
				addxxAddressItem: {},
				index: 0,
			}
		},
		components: {
			baseHead,
			pickRegions,
			addPopup
		},
		onLoad() {
			this.confirmSearch();
			this.listdata();
			this.Navigation();
		},
		computed: {
			columns1value() {
				let d = "";
				if (this.columns1.length > 0) {
					this.columns1[0].forEach(res => {
						if (res.value == this.model.Category) {
							d = res.text
						}
					})
					return d;
				} else {
					return ''
				}
			},
			columns2value() {
				let d = "";
				if (this.columns2.length > 0) {
					this.columns2[0].forEach(res => {
						if (res.value == this.model.Stage) {
							d = res.text
						}
					})
					return d;
				} else {
					return ''
				}
			},
			columns3value() {
				let arr = [];
				if (this.columns3.length > 0) {
					arr = this.columns3[0].filter(res => {
						return this.model.Scale == res.value;
					})
				}
				if (arr.length == 0) {
					return ''
				} else {
					return arr[0].text || ' '
				}
			}
		},
		methods: {
			shanchulist(delete_index) {
				if (this.model.Address.length > 1) {
					this.model.Address.splice(delete_index, 1)
				}
			}, 
			addaddlist() {
				this.model.Address.push({
					Address: "",
					Latitude: this.Latitude,
					Longitude: this.Longitude,
					PcaAddress: "",
					AreaCode: "",
					CityCode: "",
					CorpId: "",
					FullName: "",
					IsDefault: false,
					ProvinceCode: "",
					Source: 0,
				})
			},
			xxAddress(item, index) {
				this.addShow = true;
				this.addxxAddressItem = item;
				this.index = index;
			},
			//关闭地址弹窗
			editAddress(item) {
				let obj = {
					CorpId:this.model.Id,
					FullName:this.model.FullName,
					IsDefault : false
				}
				console.log(obj,'fullName-------------------')
				this.model.Address[this.index] = {...item,...obj};
				this.addShow = false;
			},
			checkboxChange(item, ind) {
				this.model.Address.forEach((res, index) => {
					if (ind == index) {
						res.IsDefault = !item.IsDefault;
					} else {
						res.IsDefault = false;
					}
				})
			},
			listdata() {
				let qyid = uni.getStorageSync('dianjiqiye');
				this.$http.core.tenantgetall(qyid.Id).then(res => {
					if (res.Success) {
						console.log(res.Result);
						this.model = res.Result;
					}
				})
			},
			confirm1(e) {
				this.model.Category = e.value[0].value;
				this.show1 = false;
			},
			confirm2(e) {
				this.model.Stage = e.value[0].value;
				this.show2 = false;
			},
			confirm3(e) {
				this.model.Scale = e.value[0].value;
				this.show3 = false;
			},

			onchange(e) {
				let OrgId = e.detail.value[e.detail.value.length - 1].value
				this.model.Industry = OrgId;
			},

			confirmSearch() {
				this.$http.core.dicAll().then(res => {
					if (res.Success) {
						res.Result.forEach(resd => {
							if (resd.EnCode == "hangye") {
								this.items5 = this.subTree(resd.DictionaryList)
							}
							if (resd.EnCode == "qylx01") {
								this.columns1 = this.xialist(resd.DictionaryList)
							}
							if (resd.EnCode == "rzjd12") {
								this.columns2 = this.xialist(resd.DictionaryList)
							}
							if (resd.EnCode == "rygm10") {
								this.columns3 = this.xialist(resd.DictionaryList)
							}
						})

					}
				})
			},
			// 下拉数据处理
			xialist(arr) {
				let drr = [];
				arr.forEach(res => {
					let obj = {
						text: res.FullName,
						value: res.EnCode
					}
					drr.push(obj)
				})
				return [drr]
			},

			//树形结构数据
			subTree(arr, fatherArr) {
				let totalArr;
				if (fatherArr !== undefined) {
					totalArr = fatherArr.children
				} else {
					totalArr = []
				}
				arr.forEach((item, index) => {
					let obj = {}
					obj.text = item.FullName
					obj.value = item.Id
					obj.children = [];
					if (item.Children !== null && item.Children.length != 0) {
						this.subTree(item.Children, obj)
					}
					totalArr.push(obj)
				})
				return totalArr
			},
			//保存
			clickRight() {
				console.log(this.model)

				if (!this.model.FullName) {
					this.$util.toast("请输入企业名称");
					return;
				}
				if (!this.model.ShortName) {
					this.$util.toast("请输入企业简称");
					return;
				}
				if (!this.model.Industry) {
					this.$util.toast("请选择行业");
					return;
				}
				if (!this.model.Category) {
					this.$util.toast("请选择企业性质");
					return;
				}
				if (!this.model.Category) {
					this.$util.toast("请选择企业性质");
					return;
				}
				if (!this.model.Stage) {
					this.$util.toast("请选择融资阶段");
					return;
				}

				if (!this.model.Scale) {
					this.$util.toast("请选择人员规模");
					return;
				}
				if (!this.model.Description) {
					this.$util.toast("请输入企业简介");
					return;
				}

				if (this.model.Address.length == 0) {
					this.$util.toast("请添加企业地址");
					return;
				}

				if (this.model.Address) {
					let type = false;
					this.model.Address.forEach(res => {
						if (!res.Address) {
							type = true
						}
					})
					if (type) {
						this.$util.toast("请选择详细地址");
						return;
					}
				}

				// console.log(this.model);
				// return;
				//修改
				this.$http.core.tenantupdate(this.model).then(res => {
					if (res.Success) {
						this.$util.toast("修改成功")
						setTimeout(() => {
							uni.navigateBack();
						}, 1000)
					}
				})
			},




			Navigation() {
				let tabt = this;
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						console.log('当前位置的经度：' + res.longitude);
						console.log('当前位置的纬度：' + res.latitude);
						tabt.Longitude = Math.abs(res.longitude);
						tabt.Latitude = Math.abs(res.latitude);
					}
				});

			}
		}
	}
</script>

<style lang="scss" scoped>
	/deep/.u-form-item__body {
		padding: 20rpx 40rpx;
	}

	/deep/.text-color {
		color: #000;
	}

	.commview {
		background-color: #ffffff;
	}

	.commtpddsview {
		padding: 20rpx;

		.adddz {
			display: flex;
			justify-content: space-between;
			padding-right: 40rpx;
		}

		.addnrdz {
			margin: 20rpx 0rpx;

			.addndax {
				display: flex;
				justify-content: space-between;
				margin-top: 20rpx;
			}
		}
	}
</style>